home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11093 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: news.ichange.com!newsmaster
  2. From: Jesse Liberty <jl@staff.ichange.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: References & Pointers in Borland C++
  5. Date: Tue, 12 Mar 1996 10:48:51 -0500
  6. Organization: AT&T
  7. Message-ID: <31459CE3.3A5D@staff.ichange.com>
  8. References: <4i26j5$roe@dfw-ixnews3.ix.netcom.com>
  9. NNTP-Posting-Host: 140.244.99.60
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win95; I)
  14. CC: jl@staff.ichange.com
  15.  
  16. Bob M wrote:
  17. > I am trying to understand the concept of "References".
  18. > But a couple of things are confusing me.
  19. > 1.
  20. > The Programmer's manuals for Borland Turbo C++ version 1.01 (1990)
  21. > and Borland C++ version 4.02 describe in nearly identical text, that
  22. > it is acceptable to initialize a Reference with a specific number:
  23. > int& ir = 6;
  24. > Both my 3.1 and 4.02 compilers reject this with an error: "Reference
  25. > initialized with 'int' needs lvalue of type 'int'".
  26. > Which is right, the compiler or the book?
  27.  
  28. The compiler is right. You can write
  29.     const int& ir = 6;
  30.  
  31. But having a non const integer reference would allow you later to write
  32.  
  33.     ir = 7;
  34.  
  35. and what would that do? change the constant integer 6 to 7??
  36.  
  37.  
  38.  
  39. > 2.
  40. > Using the Inspector window in the Debugger, I could see that a
  41. > Reference appears identical to a pointer, except that the compiler
  42. > insists that you initialize it to some selected variable.
  43. > It looks as though one should be able to assign a "filled in"
  44. > pointer to a reference, as follows:
  45. > int num, nother;
  46. > int &ref1 = num, &ref2 = nother;
  47. > int *ptr1;
  48. > ptr1 = &ref1;  // sets the pointer to the same address as "num".
  49. > &ref2 = ptr1;  // Change &ref2 to point at "num".
  50. > The first assignment is apparently legal, but the second depends
  51. > on the version of the compiler. 3.1 accepts it, while 4.01 says:
  52. > "Lvalue required in function main".
  53. > Should this be legal?
  54.  
  55. Right, because your first line is not doing what you think. When you write
  56.  
  57. ptr1 = &ref1;
  58.  
  59. what you are really writing is
  60.  
  61. ptr1 = #  // remember ref1 is an alias to num
  62.  
  63. which is perfectly legal.
  64.  
  65. When you then write
  66.  
  67. &ref2 = ptr1;
  68.  
  69. What you are really writing is
  70.  
  71. ¬her = ptr1; 
  72.  
  73. This is not legal, you can't assign to the address of a variable.
  74.  
  75.  
  76. The fact that references are generally implemented using pointers is irrelevant; they are a different data type with explicit 
  77. rules.
  78.